package com.sunlands.zlcx.usercenter.service;

import com.sunlands.zlcx.usercenter.domain.BuryingPointDO;
import com.sunlands.zlcx.usercenter.exception.BuryingPointException;
import com.sunlands.zlcx.usercenter.exception.SendExceptionUtil;
import com.sunlands.zlcx.usercenter.repository.BuryingPointRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author anjl
 * @date 6:23 PM 2019/7/11
 * description:
 */
@Slf4j
@Service
public class BuryingPointServiceImpl {
    private static final BlockingQueue<BuryingPointDO> pointQueue = new LinkedBlockingDeque<>(100000);

    @Autowired
    private SendExceptionUtil sendExceptionUtil;

    @Transactional
    public void save(List list){
        buryingPointRepository.save(list);
    }

    public void add(BuryingPointDO buryingPointDO){
        boolean result = pointQueue.offer(buryingPointDO);
        if(!result){
            log.error("埋点添加元素失败， result = {}", false);
            sendExceptionUtil.sendException("埋点添加元素失败", new BuryingPointException());
        }
    }

    public List<BuryingPointDO> drainTo(int count){
        List<BuryingPointDO> list = new ArrayList<>();
        pointQueue.drainTo(list, 10000);
        return list;
    }



    @Autowired
    private BuryingPointRepository buryingPointRepository;


}
